package com.itheima.service.Impl;

import com.itheima.dao.MemberMapper;
import com.itheima.dto.LoginDTO;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 登录
     * @param loginDTO
     * @return
     */
    public boolean login(LoginDTO loginDTO) {
        String validateCode = (String)redisTemplate.opsForValue().get("login_" + loginDTO.getTelephone());

        if(validateCode != null && validateCode.equals(loginDTO.getValidateCode())){
            redisTemplate.delete("login_"+loginDTO.getTelephone());
            Member member = memberMapper.getByTelephone(loginDTO.getTelephone());
            if(member == null){
                member = new Member();
                member.setPhoneNumber(loginDTO.getTelephone());
                member.setRegTime(new Date());
                memberMapper.insert(member);
            }
            return true;
        }else {
            return false;
        }

    }
}
